import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { PostRiskEventScheme, IPostRiskEventQueryParams, postRiskEventStatusMap } from '@shared/interfaces/postRisk-event'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { ISearchSchema, ISearchOption } from '@shared/interfaces/search'
import { STColumn } from '@delon/abc'
import { ESearchType, EStatus } from '@shared/enums'
import { IAamsItem } from '@shared/components/aams-select/aams-select.interface'
import { StatusTextMap, RiskTypeMap } from '@shared/interfaces/common'
import { PostRiskEventService } from '../post-risk-event.service'
import { ModalService } from '@shared/services/modal.service'
import { SettingsService } from '@delon/theme'

@Component({
  selector: 'app-post-risk-event',
  templateUrl: './list.component.html',
  styleUrls: ['../../../shared/styles/table.less'],
})
export class PostRiskEventListComponent extends ListBaseClass<PostRiskEventScheme, IPostRiskEventQueryParams>
  implements OnInit {
  @ViewChild('tplAcquirer') tplAcquirer: TemplateRef<void>
  @ViewChild('tplGroup') tplGroup: TemplateRef<void>
  @ViewChild('tplMerchant') tplMerchant: TemplateRef<void>
  @ViewChild('tplStore') tplStore: TemplateRef<void>

  selectedAcquirer: IAamsItem

  postRiskEventStatusMap = postRiskEventStatusMap

  RiskTypeMap = RiskTypeMap

  riskCategoryOptions: ISearchOption[] = [
    { label: this.trans('RISK_CTRL_CATEGORY.NORMAL'), value: 'normal' },
    { label: this.trans('RISK_CTRL_CATEGORY.SPECIAL'), value: 'special' },
  ]
  statusOptions = [
    { label: this.trans('GENERAL.STATUS.NORMAL'), value: 'normal' },
    { label: this.trans('GENERAL.STATUS.FREEZE'), value: 'freeze' },
  ]

  searchSchema: ISearchSchema[] = []

  customSchema: ISearchSchema[] = []

  data: PostRiskEventScheme[] = []
  columns: STColumn[] = [
    {
      type: 'link',
      title: '风控事件编号',
      i18n: 'POST_RISK_EVENT.ID',
      index: 'eventID',
      width: '150px',
      className: 'text-center',
      fixed: 'left',
      click: this.goDetail.bind(this),
    },
    {
      title: '机构编号',
      i18n: 'GENERAL.INSTITUTION.CODE',
      index: 'insCode',
      width: '150px',
      className: 'text-left',
    },
    {
      title: '门店编号',
      i18n: 'GENERAL.STORE.NUM',
      index: 'storeNum',
      className: 'text-center',
      width: '150px',
      render: 'type',
    },
    {
      title: '门店名称',
      i18n: 'GENERAL.STORE.NAME',
      index: 'storeName',
      className: 'text-center',
      width: '100px',
    },
    {
      title: '门店英文名称',
      i18n: 'GENERAL.STORE.NAME_EN',
      index: 'storeNameEN',
      className: 'text-center',
      width: '200px',
    },
    {
      title: '创建时间',
      i18n: 'GENERAL.INFO.CREATED_AT',
      index: 'createTime',
      className: 'text-center',
      width: '200px',
      type: 'date',
      dateFormat: 'YYYY-MM-DD HH:mm',
    },
    {
      title: '风控评分',
      i18n: 'RISK_EVENT.SCORE',
      index: 'storeScore',
      className: 'text-center',
      width: '100px',
      render: 'storeScore',
    },
    {
      title: '状态',
      i18n: 'GENERAL.INFO.STATUS',
      index: 'handleStatus',
      className: 'text-center',
      width: '100px',
      render: 'handleStatus',
    },
    {
      title: '更新时间',
      i18n: 'GENERAL.INFO.UPDATED_AT',
      index: 'updateTime',
      className: 'text-center',
      width: '100px',
      type: 'date',
      dateFormat: 'YYYY-MM-DD HH:mm',
      fixed: 'right'
    },
  ]

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    private modalService: ModalService,
    public postRiskEventService: PostRiskEventService,
    public setting: SettingsService,
  ) {
    super(msg, i18n, route, router, setting)
  }

  ngOnInit() {
    this.initSearchParams()
    this.parseParams().subscribe(() => {
      this.queryTableDataWithParams()
    })
    this.initialAAMSFilter()
  }

  initialAAMSFilter() {
    this.initAcquirer()
    this.initAgent()
    this.initMerchant()
    this.initStore()
  }

  initSearchParams(): void {
    this.searchSchema = [
      {
        label: 'GENERAL.INFO.CREATED_AT',
        labelI18n: 'GENERAL.INFO.CREATED_AT',
        index: ['createdAtFrom', 'createdAtTo'],
        type: ESearchType.dateRange,
        value: [],
      },
      {
        label: 'GENERAL.INFO.UPDATED_AT',
        labelI18n: 'GENERAL.INFO.UPDATED_AT',
        index: ['updatedAtFrom', 'updatedAtTo'],
        type: ESearchType.dateRange,
        value: [],
      },
      {
        label: 'RISK_EVENT.SCORE',
        labelI18n: 'RISK_EVENT.SCORE',
        index: 'storeScore',
        type: ESearchType.input,
      },
      {
        label: 'GENERAL.INFO.STATUS',
        labelI18n: 'GENERAL.INFO.STATUS',
        index: 'status',
        type: ESearchType.selection,
        options: [
          { label: this.trans('RISK.EVENT.STATUS.PENDING'), value: 'P' },
          { label: this.trans('RISK.EVENT.STATUS.EXAMINED'), value: 'E' },
          { label: this.trans('RISK.EVENT.STATUS.RELEASED'), value: 'R' },
        ],
      },
      {
        label: 'GENERAL.BIZ_LEVEL.INSTITUT ION',
        labelI18n: 'GENERAL.BIZ_LEVEL.INSTITUTION',
        index: 'insCode',
        component: this.tplAcquirer,
        customFunc: () => this.selectedAcquirer && this.selectedAcquirer.code,
      },
      {
        label: 'GENERAL.BIZ_LEV EL.AGENT',
        labelI18n: 'GENERAL.BIZ_LEVEL.AGENT',
        index: 'agentCode',
        component: this.tplGroup,
        customFunc: () => this.selectedGroup && this.selectedGroup.code,
      },
      {
        label: 'GENERAL.BIZ_L EVEL.MERCHANT',
        labelI18n: 'GENERAL.BIZ_LEVEL.MERCHANT',
        index: 'intMerCode',
        component: this.tplMerchant,
        customFunc: () => this.selectedMerchant && this.selectedMerchant.code,
      },
      {
        label: 'GENER AL.BIZ_LEVEL.STORE',
        labelI18n: 'GENERAL.BIZ_LEVEL.STORE',
        index: 'intStoreCode',
        component: this.tplStore,
        customFunc: () => this.selectedStore && this.selectedStore.code,
      },
    ]
  }

  goAdd(): void {
    this.router.navigate(['../new'], { relativeTo: this.route })
  }

  goDetail(scheme: PostRiskEventScheme): void {
    const { eventID, intStoreCode , storeScore, handleStatus} = scheme
    this.router.navigate([`../${eventID}/${intStoreCode}/${storeScore}/${handleStatus}`], { relativeTo: this.route })
  }

  renderList(res): void {
    const { data, total } = res
    this.data = data || []
    this.total = total
    this.loading = false
  }

  queryTableDataWithParams(): void {
    this.loading = true
    this.postRiskEventService.getList(this.params).subscribe(this.renderList.bind(this), this.getListError.bind(this))
  }
}
